//
//  EditDataViewController.m
//  MiaoTu
//
//  Created by dllo on 16/8/30.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "EditDataViewController.h"
#import "Masonry.h"
#import "MTMacro.h"
#import "UIView+Frame.h"
#import "HeadImageView.h"
#import "EditDataTableCell.h"
#import "EditDataModel.h"
#import "CommonLabelCollectionReusableView.h"
#import "MyLabelCollectionCell.h"
#import "NSString+isBlankString.h"

#define kEditDataTableCell @"EditDataTableCellIdentifier"
#define kCommonLabelCollectionReusableView @"CommonLabelCollectionReusableIdentifier"
#define kMyLabelCollectionCell @"MyLabelCollectionCellIdentifier"

@interface EditDataViewController ()
<   UITableViewDataSource,
    UITableViewDelegate,
    UICollectionViewDataSource,
    UICollectionViewDelegate,
    UICollectionViewDelegateFlowLayout,
    CommonLabelCollectionReusableViewDelegate,
    UICollectionViewDelegateFlowLayout
>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HeadImageView *headImageView;
@property (nonatomic, strong) NSMutableArray<EditDataModel *> *editDataArray;
@property (nonatomic, strong) UICollectionView *myLabelCollectionView;
@property (nonatomic, strong) NSMutableArray *myLabelArray;
@end

@implementation EditDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"编辑个人资料";
    self.view.backgroundColor = [UIColor colorWithRed:0.929 green:0.941 blue:0.957 alpha:1.000];
    
    
    [self createTableView];
    UIImageView *pencilImageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 25, 25)];
    pencilImageView.layer.cornerRadius = 25 / 2.f;
    pencilImageView.layer.masksToBounds = YES;
    [self.view addSubview:pencilImageView];
    pencilImageView.image = [UIImage imageNamed:@"editPencil"];
    pencilImageView.backgroundColor = [UIColor colorWithRed:0.510 green:0.843 blue:0.843 alpha:1.000];
    [self createData];
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, self.view.dk_height - 64 - 49 - 20) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.layer.cornerRadius = 8;
    _tableView.layer.masksToBounds = YES;
    _tableView.tableHeaderView = [self createTableHeaderView];
    _tableView.tableFooterView = [self createTableFooterView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.showsVerticalScrollIndicator = NO;
    [_tableView registerNib:[UINib nibWithNibName:@"EditDataTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kEditDataTableCell];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (UIView *)createTableHeaderView {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.dk_width, 148)];
    self.headImageView = [[HeadImageView alloc] initWithImage:[UIImage imageNamed:@"defaultHeadImage"]];
    [headerView addSubview:_headImageView];
    [_headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(headerView);
        make.width.height.mas_equalTo(headerView.mas_width).multipliedBy(0.2f);
    }];
    
    UILabel *desclabel = [[UILabel alloc] init];
    desclabel.text = @"添加头像(必填)";
    desclabel.font = [UIFont systemFontOfSize:15.f];
    desclabel.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:desclabel];
    [desclabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(headerView);
        make.top.mas_equalTo(_headImageView.mas_bottom);
        make.bottom.mas_equalTo(headerView.mas_bottom);
    }];
    return headerView;
}

- (UIView *)createTableFooterView {
    
    UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
    fl.sectionInset = UIEdgeInsetsMake(0, 0, 10, 60);
//    fl.itemSize = CGSizeMake(60, 52);
    fl.minimumInteritemSpacing = 10;
    self.myLabelCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(30, 0, 0, 220) collectionViewLayout:fl];
    _myLabelCollectionView.backgroundColor = [UIColor whiteColor];
    _myLabelCollectionView.dataSource = self;
    _myLabelCollectionView.delegate = self;
    _myLabelCollectionView.bounces = NO;
    _myLabelCollectionView.scrollEnabled = NO;
    [_myLabelCollectionView registerClass:[CommonLabelCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCommonLabelCollectionReusableView];
    [_myLabelCollectionView registerNib:[UINib nibWithNibName:@"MyLabelCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kMyLabelCollectionCell];
    self.myLabelArray = [NSMutableArray array];
    return _myLabelCollectionView;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    CommonLabelCollectionReusableView *reuseView = [_myLabelCollectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCommonLabelCollectionReusableView forIndexPath:indexPath];
    reuseView.delegate = self;
    return reuseView;
}

- (void)sendLabelValue:(NSString *)myLabelString {
    if ([myLabelString isBlankString]) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 30)];
        label.text = @"标签不能为空格!";
        label.textAlignment = NSTextAlignmentCenter;
        label.center = self.view.center;
        label.layer.cornerRadius = 10;
        label.layer.masksToBounds = YES;
        label.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:label];
        label.alpha = 0;
        [UIView animateWithDuration:1 animations:^{
            label.alpha = 1;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:1 animations:^{
                label.alpha = 0;
            }];
        }];
        return;
    }    
    if (NSNotFound == [_myLabelArray indexOfObject:myLabelString]) {
        [_myLabelArray addObject:myLabelString];
        
        [_myLabelCollectionView reloadData];
        // reloadData 之后执行的操作
        [_myLabelCollectionView performBatchUpdates:^{
            
        } completion:^(BOOL finished) {
            _myLabelCollectionView.dk_height = _myLabelCollectionView.contentSize.height;
            [_tableView beginUpdates];
            _tableView.tableFooterView = _myLabelCollectionView;
            [_tableView endUpdates];
        }];
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [_myLabelArray removeObjectAtIndex:indexPath.item];
    [collectionView deleteItemsAtIndexPaths:@[indexPath]];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(0, 220);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _myLabelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyLabelCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kMyLabelCollectionCell forIndexPath:indexPath];
    cell.myLabel.text = _myLabelArray[indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGRect newRect = [_myLabelArray[indexPath.row] boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 50) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];
    newRect.size.height += 10;
    newRect.size.width += 40;
    return newRect.size;
}

- (void)createData {
    NSArray *editDicArray = @[
    @{@"title": @"昵称", @"placeholder": @"请输入您的昵称(必填)"},
    @{@"title": @"性别", @"placeholder": @"请输入您的性别(必填)"},
    @{@"title": @"年龄", @"placeholder": @""},
    @{@"title": @"情感状态", @"placeholder": @"请输入您的情感状态(必填)"},
    @{@"title": @"想去", @"placeholder": @"请输入您想去的地方(必填)"},
    @{@"title": @"家乡", @"placeholder": @"如杭州(选填)"},
    @{@"title": @"职业", @"placeholder": @"如产品狗(选填)"},
    @{@"title": @"学校", @"placeholder": @"如清华大学(选填)"},
    @{@"title": @"生活在", @"placeholder": @"如北京(选填)"},
    @{@"title": @"工作在", @"placeholder": @"如北京中关村(选填)"},
    @{@"title": @"有闲", @"placeholder": @"2015-12-23(选填)"},
    @{@"title": @"约游预算", @"placeholder": @"2K-4K(选填)"},
    ];
    
    self.editDataArray = [NSMutableArray array];
    for (NSDictionary *dic in editDicArray) {
        EditDataModel *model = [EditDataModel modelWithDic:dic];
        [_editDataArray addObject:model];
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _editDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EditDataTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kEditDataTableCell forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.editData = _editDataArray[indexPath.row];
    if (indexPath.row == 2) {
        cell.infoTextField.text = @"20";
    }
    return cell;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
